﻿using B.Auth.APi.Application.Command.Role;
using B.Auth.APi.Application.Command.Role.DTO;
using B.Auth.Domain.Entity;
using B.Auth.Domain.Repository;
using B.Auth.Enum;
using MediatR;

namespace B.Auth.APi.Application.CommandHand.Role
{
    public class RoleTreeCommandHand : IRequestHandler<RoleTreeCommand, ApiResult<List<RoleTreeDto>>>
    {

        private readonly IBaseRepository<RoleModel> _role;

        public RoleTreeCommandHand(IBaseRepository<RoleModel> role)
        {
            _role = role;
        }

        public Task<ApiResult<List<RoleTreeDto>>> Handle(RoleTreeCommand request, CancellationToken cancellationToken)
        {
            var result=new ApiResult<List<RoleTreeDto>>();
           var list=new List<RoleTreeDto>();
            var lis = _role.GetAll().Where(x=>x.Del==0).ToList();
            foreach (var item in lis)
            {

                list.Add(new()
                {

                    value = item.Id,
                    label = item.RoleName,
                    children =null
                });

            };
            result.data = list;
            return Task.FromResult(result);
            
        }
    }
}
